import { defineStore } from "pinia";
import { parse, stringify } from "zipson";
import { getInfo } from "@/api/getList";
const useUserStore = defineStore("user", {
    state: (): { info: object; roles: Array<string>; permissions: Array<string> } => ({
        info: {
            name: "user",
            age: 18,
            sex: "男",
            address: "中国",
        },
        roles: [],
        permissions: [],
    }),
    actions: {
        setUserInfo(userInfo: any) {
            this.info = userInfo;
        },
        getInfo() {
            return new Promise((resolve, reject) => {
                getInfo({ role: "financial" })
                    .then((res: any) => {
                        if (res.data.code === 200) {
                            const data = res.data.data;
                            if (data.roles && data.roles.length > 0) {
                                this.roles = data.roles;
                                this.permissions = data.permissions;
                            } else {
                                this.roles = ["ROLE_DEFAULT"];
                            }
                        }
                        resolve(res);
                    })
                    .catch(err => reject(err));
            });
        },
    },
    persist: {
        key: "user",
        storage: sessionStorage,
        paths: ["info"],
        serializer: {
            deserialize: parse,
            serialize: stringify,
        },
    },
});
export default useUserStore;
